「SpringBoot3 Web开发」模板引擎 | 您所在的位置:网站首页 › springboot 模板引擎 html 压缩一行 › 「SpringBoot3 Web开发」模板引擎 |
文章目录
1. Thymeleaf整合2. 基础语法1. 核心用法2. 语法示例
3. 属性设置4. 遍历5. 判断th:ifth:switch
6. 属性优先级7. 行内写法8. 变量选择9. 模板布局10. devtools
由于 SpringBoot 使用了嵌入式 Servlet 容器。所以 JSP 默认是不能使用的。如果需要服务端页面渲染,优先考虑使用 模板引擎。
模板引擎页面默认放在 src/main/resources/templatesSpringBoot 包含以下模板引擎的自动配置 FreeMarkerGroovyThymeleafMustacheThymeleaf官网:https://www.thymeleaf.org/ DOCTYPE html> Good Thymes Virtual Grocery Welcome to our grocery store! 原内容 登录**表达式**:用来动态取值 **${}**:变量取值;使用model共享给页面的值都直接用${}**@{}**:url路径;#{}:国际化消息~{}:片段引用*{}:变量选择:需要配合th:object绑定对象系统工具&内置对象:详细文档 param:请求参数对象session:session对象application:application对象#execInfo:模板执行信息#messages:国际化消息#uris:uri/url工具#conversions:类型转换工具#dates:日期工具,是java.util.Date对象的工具类#calendars:类似#dates,只不过是java.util.Calendar对象的工具类#temporals: JDK8+ java.time API 工具类#numbers:数字操作工具#strings:字符串操作#objects:对象操作#bools:bool操作#arrays:array工具#lists:list工具#sets:set工具#maps:map工具#aggregates:集合聚合工具(sum、avg)#ids:id生成工具 2. 语法示例表达式: 变量取值:${…}url 取值:@{…}国际化消息:#{…}变量选择:*{…}片段引用: ~{…}常见: 文本: ‘one text’,‘another one!’,…数字: 0,34,3.0,12.3,…布尔:true、falsenull: null变量名: one,sometext,main…文本操作: 拼串: +文本替换:| The name is ${name} |布尔操作: 二进制运算: and,or取反:!,not比较运算: 比较:>,原内容 登录 4. 遍历语法: th:each="元素名,迭代状态 : ${集合}" Onions 2.41 yes Onions 2.41 yesiterStat 有以下属性: index:当前遍历元素的索引,从0开始count:当前遍历元素的索引,从1开始size:需要遍历元素的总数量current:当前正在遍历的元素对象even/odd:是否偶数/奇数行first:是否第一个元素last:是否最后一个元素 5. 判断 th:if view User is an administrator User is a manager User is some other thing 6. 属性优先级 片段遍历判断 Item description here... OrderFeatureAttributes1片段包含th:insert th:replace2遍历th:each3判断th:if th:unless th:switch th:case4定义本地变量th:object th:with5通用方式属性修改th:attr th:attrprepend th:attrappend6指定属性修改th:value th:href th:src …7文本值th:text th:utext8片段指定th:fragment9片段移除th:remove 7. 行内写法[[...]] or [(...)] Hello, [[${session.user.name}]]! 8. 变量选择 Name: Sebastian. Surname: Pepper. Nationality: Saturn.等同于 Name: Sebastian. Surname: Pepper. Nationality: Saturn. © 2011 The Good Thymes Virtual Grocery 结果: © 2011 The Good Thymes Virtual Grocery © 2011 The Good Thymes Virtual Grocery 10. devtools org.springframework.boot spring-boot-devtools修改页面后;ctrl+F9刷新效果;java代码的修改,如果devtools热启动了,可能会引起一些bug,难以排查 |
CopyRight 2018-2019 实验室设备网 版权所有 |